//
//  BinaryInfoViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/10.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class BinaryInfoViewController: BaseViewController {

    var url : String = ""
    var phoneView : PhoneLabelView?
    var serial : SerialView?
    var testing : AutoTestingView?
    var function : FunctionTestView?
    var mod : PhoneInfoModel?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviHide = false
        self.title = "二维码信息"
        self.view.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        self.defaultBackButton()
        loadData()
    }
    
    override func pop() {
        let _ = self.navigationController?.popToRootViewController(animated: true)
    }
    
    func configView(model : PhoneInfoModel) -> Void {
        let scroll = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: YXDeviceWidth, height: YXDeviceHeight - 140 * YXHeightScale))
        scroll.showsVerticalScrollIndicator = false
        scroll.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        self.view.addSubview(scroll)
        phoneView = PhoneLabelView.init(lbl: model.phone_model, frame: CGRect.init(x: 0, y: 0, width: YXDeviceWidth, height: 198 * YXHeightScale))
        scroll.addSubview(phoneView!)//done
        
        let back = UIView.init(frame: CGRect.init(x: 10 * YXWidthScale, y: phoneView!.bottom() + 6 * YXHeightScale, width: 730 * YXWidthScale, height: 0))
        scroll.addSubview(back)
        
        testing = AutoTestingView.init(frame: CGRect.init(x: 0, y: 0, width: 730 * YXWidthScale, height: 400))
        //将model打包成array
        let arr = PhoneContentGenerator.array(model: model)
        testing!.loadContent(title: "检测报告", message: "基本信息", infos: arr)
        back.addSubview(testing!)//done
        back.frame = CGRect.init(x: 10 * YXWidthScale, y: phoneView!.bottom() + 6 * YXHeightScale, width: 730 * YXWidthScale, height: testing!.height())
        back.backgroundColor = testing!.backgroundColor
        let back1 = UIView.init(frame: CGRect.init(x: 10 * YXWidthScale, y: back.bottom(), width: 730 * YXWidthScale, height: 0))
        function = FunctionTestView.init(frame: CGRect.init(x: 42 * YXWidthScale, y: 64 * YXHeightScale, width: 636 * YXWidthScale, height: 528 * YXHeightScale))
        function!.content = PhoneContentGenerator.flagArr(model: model)//done
        function!.configContent()
        back1.addSubview(function!)
        scroll.addSubview(back1)
        
        back1.frame = CGRect.init(x: 10 * YXWidthScale, y: back.bottom(), width: 730 * YXWidthScale, height: function!.height() + 220 * YXHeightScale)
        back1.backgroundColor = back.backgroundColor
        
        let mask = UIView.init(frame: CGRect.init(x: 0, y: back.bottom() - 10 * YXHeightScale, width: 20 * YXHeightScale, height: 20 * YXHeightScale))
        mask.backgroundColor = scroll.backgroundColor
        mask.layer.cornerRadius = 10 * YXHeightScale
        scroll.addSubview(mask)
        
        let mask1 = UIView.init(frame: CGRect.init(x: YXDeviceWidth - 20 * YXWidthScale, y: back.bottom() - 10 * YXWidthScale, width: 20 * YXWidthScale, height: 20 * YXWidthScale))
        mask1.backgroundColor = scroll.backgroundColor
        mask1.layer.cornerRadius = 10 * YXWidthScale
        scroll.addSubview(mask1)
        scroll.contentSize = CGSize.init(width: 0, height: back1.bottom() + 64)
        
        let trayBottom = UIView.init(frame: CGRect.init(x: 0, y: YXDeviceHeight - 64 - 140 * YXHeightScale, width: YXDeviceWidth, height: 140 * YXHeightScale))
        trayBottom.layer.borderColor = UIColor.colorWithHexString(hex: "f2f2f2").cgColor
        trayBottom.layer.borderWidth = YXHeightScale
        trayBottom.backgroundColor = UIColor.white
        self.view.addSubview(trayBottom)
        let gen = UIButton.init(type: .system)
        gen.frame = CGRect.init(x: 135 * YXWidthScale, y: 16 * YXHeightScale, width: 480 * YXWidthScale, height: 108 * YXHeightScale)
        trayBottom.addSubview(gen)
        gen.backgroundColor = UIColor.black
        gen.layer.cornerRadius = YXButtonCorner
        gen.setTitle("继续检测外观", for: .normal)
        gen.setTitleColor(UIColor.white, for: .normal)
        gen.addTarget(self, action: #selector(generate), for: .touchUpInside)
        trayBottom.addSubview(gen)
    }

    @objc func generate()->Void{
        let checkVC = CheckViewController.init()
        checkVC.mod = mod
        self.navigationController?.pushViewController(checkVC, animated: true)
    }
    
    func loadData() -> Void {

        let reqURL = URL.init(string: url)!
        let session = URLSession.shared
        self.beginAnim()
        let task = session.dataTask(with: reqURL, completionHandler: {(data,response,error) in
            self.endAnim()
            guard data != nil else{
                return
            }
            do{
                let dict = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? Dictionary<String,Any>
                let model = PhoneInfoModel.init(source: dict)
                if model.id != -1 {
                    self.mod = model
                    DispatchQueue.main.async {
                        self.configView(model: model)
                    }
                } else {
                    DispatchQueue.main.async {
                        self.commonAlertMsg(content: "该二维码并无手机数据!")
                    }
                }
            }catch{
                DispatchQueue.main.async {
                    self.commonAlertMsg(content: "该二维码非法!")
                }
            }
        })
        task.resume()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
